#include "Thread.h"

Thread::Thread()
{
    _threadId  = 0;
    _threadNum = 0;
}

Thread::~Thread()
{
}

void* Thread::Func(void* obj)
{
    Thread* th = (Thread*)obj;

    return th->Process();
}

pthread_t Thread::GetThreadID()
{
    return _threadId;
}

int32 Thread::GetThreadNum()
{
    return _threadNum;
}

int32 Thread::Run(int32 threadNum)
{
    if(threadNum < 0)
    {
        WriteLog(FATAL, "Thread Num[%d] must bigger or equal to 0.", threadNum);
        exit(-1);
    }

    _threadNum = threadNum;

    int ret = pthread_create(&_threadId, NULL, Thread::Func, this);

    if(ret != 0)
    {
        WriteLog(FATAL, "Create thread error[%d].", ret);
        exit(-1);
    }

    return ret;
}

int32 Thread::Join()
{
    int ret = pthread_join(_threadId, NULL);

    if(ret == 0)
    {
        _threadId = 0;
    }

    return ret;
}
